﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using MsDAL;

namespace UIMarker
{
    public partial class FrmPageSource : Form
    {
        string insertTmp = @"INSERT INTO Sys5a02([listid],[plistid],[action],[pkfield],[ptable],[pfield],[pwhere],[porder],[remark]) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}');";
        string updateTmp = @"UPDATE Sys5a02 SET [plistid] = '{1}',
[action] = '{2}',
[pkfield] = '{3}',
[ptable] = '{4}',
[pfield] = '{5}',
[pwhere] = '{6}',
[porder] = '{7}',
[remark] = '{8}'
WHERE [listid] = '{0}'; ";

        Sys5a02DAL dal = new Sys5a02DAL();
        private Sys5a02 tModel = new Sys5a02();
        string plistid = "";

        public FrmPageSource(Sys5a02 _tmodel)
        {
            InitializeComponent();

            tModel = _tmodel;
            if (!string.IsNullOrEmpty(tModel.Listid)) LoadEdit();
            else if (string.IsNullOrEmpty(tModel.Listid) && !string.IsNullOrEmpty(tModel.Ptable))  //修改模式下第二次点入
            {
                this.ptable.Text = tModel.Ptable;
                this.pwhere.Text = tModel.Pwhere;
                this.remark.Text = tModel.Remark;
                this.action.Text = tModel.Action;
                this.pfield.Text = tModel.Pfield;
                this.pkfield.Text = tModel.Pkfield;
                this.porder.Text = tModel.Porder;
                plistid = tModel.Plistid;
            }
        }

        private void LoadEdit()
        {
            Sys5a02 model = null;
            if (string.IsNullOrEmpty(tModel.Ptable)) //修改模式下第一次点入
            {
                model = dal.GetModelById(tModel.Listid);
            }
            else
            {
                model = tModel; //修改模式下再次点入
            }
            if (model == null) return;

            this.ptable.Text = model.Ptable;
            this.pwhere.Text = model.Pwhere;
            this.remark.Text = model.Remark;
            this.action.Text = model.Action;
            this.pfield.Text = model.Pfield;
            this.pkfield.Text = model.Pkfield;
            this.porder.Text = model.Porder;
            plistid = model.Plistid;
        }

        private void tspClose_Click(object sender, EventArgs e)
        {
            tModel = null;
            this.Close();
        }

        private void tspbtnSave_Click(object sender, EventArgs e)
        {
            tModel.Ptable = this.ptable.Text;
            tModel.Pwhere = this.pwhere.Text;
            tModel.Remark = this.remark.Text;
            tModel.Action = this.action.Text;
            tModel.Pfield = this.pfield.Text;
            tModel.Pkfield = this.pkfield.Text;
            tModel.Porder = this.porder.Text;

            this.Close();
        }

        /// <summary>
        /// 得到Sys5a02
        /// </summary>
        /// <returns></returns>
        public Sys5a02 GetEditModel()
        {
            return tModel;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ToolStripButton s = sender as ToolStripButton;

            string temp = s == toolStripButton3 ? insertTmp : updateTmp;

            tModel.Ptable = this.ptable.Text;
            tModel.Pwhere = this.pwhere.Text;
            tModel.Remark = this.remark.Text;
            tModel.Action = this.action.Text;
            tModel.Pfield = this.pfield.Text;
            tModel.Pkfield = this.pkfield.Text;
            tModel.Porder = this.porder.Text;

            string cnt = string.Format(temp,
                tModel.Listid, plistid, tModel.Action, tModel.Pkfield, tModel.Ptable.Replace("'", "''"),
                tModel.Pfield.Replace("'", "''"), tModel.Pwhere.Replace("'", "''"), tModel.Porder, tModel.Remark);

            //将内容复制到剪切板中
            Clipboard.SetDataObject(cnt);
        }
    }
}
